<?php
class workController extends Controller_Base
{
    public function init()
    {
        $this->noLogInJump();
    }
    
    public function indexAction()
    {
        $this->listAction();
    }

    public function listAction()
    {
        $page = intval($this->getParam('page', 1));
        if($page <= 0) $page = 1;
        
        try {
            if(Model_User::getLevelID() == 1){
                $cnt = Model_Work::getCntByModelName(Model_User::getUserName());
                $works = Model_Work::getByModelName(Model_User::getUserName(), $page);
            }else if(Model_User::getLevelID() == 3){
                $cnt = Model_Work::getCntByUserName(Model_User::getUserName());
                $works = Model_Work::getByUserName(Model_User::getUserName(), $page);
            }else{
                $cnt = Model_Work::getCount();
                $works = Model_Work::getList($page);
            }
            
            foreach($works as $key=>$work){
                $works[$key]['worktype'] = Model_Common::getWorkTypeName($work['worktype']);
            }
            $pageNav = Model_Navigate::getPageListBar($cnt, $page, Config_Default::getWorkPageSize());
        }catch (Exception $e){
            $this->errExit($e->getTraceAsString(), $e->getMessage());
        }
        $this->setData('title', '工作列表');
        $this->setData('worklist', true);
        $this->setData('pageNav', $pageNav);
        $this->setData('works', $works);
        $this->setView('Work/list.tpl');
    }
    
    public function addAction()
    {
        if(Model_User::getLevelID() == 1){
            $this->notAuthWeb();
            return;
        }
        
        try {
            $workType = Config_Default::getWorkType();
        }catch (Exception $e){
            $this->errExit($e->getTraceAsString(), $e->getMessage());
        }
        $this->setData('title', '添加工作');
        $this->setData('workadd', true);
        $this->setData('workType', $workType);
        $this->setView('Work/add.tpl');
    }
    
    public function modifyAction()
    {
        try {
        	
	        $workID = intval($this->getParam('workid', -1));
	        $workinfo = Model_Work::getByID($workID);
        	if(empty($workinfo) || !Model_Work::ifAuthModify($workinfo)){
	            $this->notAuthWeb();
	            return;
	        }
	        $ref = Model_Common::getRef();
            $models = Model_Model::getAll();
            $work = Model_Work::getByID($workID);
            $work['modelids'] = Model_Model::getIDsByNameStr($work['modelname']);
            $workType = Config_Default::getWorkType();
        }catch (Exception $e){
            $this->errExit($e->getTraceAsString(), $e->getMessage());
        }
        $this->setData('title', '编辑工作');
        $this->setData('work', $work);
        $this->setData('models', $models);
        $this->setData('ref', $ref);
        $this->setData('workType', $workType);
        $this->setView('Work/modify.tpl');
    }
    
    public function detailAction()
    {
        $workID = intval($this->getParam('workid', -1));
        $ref = Model_Common::getRef();
        $refArr = explode('/', $ref);
        if(isset($refArr[4]) && $refArr[4] == 'modify'){
        	$ref = 'http://' . HOST . '/work/list';
        }
        try {
            $work = Model_Work::getByID($workID);
            if(empty($work)){
                $this->notExistPage('工作不存在', '您访问的工作不存在，即将跳转到首页');
                return ;
            }
            
            $workType = Config_Default::getWorkType();
            $work['flg'] = $work['flg'] == 1?"面试":"工作";
            $work['worktype'] = $workType[$work['worktype']];
        }catch (Exception $e){
            $this->errExit($e->getTraceAsString(), $e->getMessage());
        }
        $this->setData('title', '工作详情');
        $this->setData('work', $work);
        $this->setData('ref', $ref);
        $this->setView('Work/detail.tpl');
    }
    
    public function doAddAction()
    {
        if(Model_User::getLevelID() == 1){
            $this->notAuthAjax();
            return;
        }
        if(Model_User::getLevelID() == 5){
        	$username = $this->getPost('username', '');
        }else{
        	$username = Model_User::getUserName();
        }
        $sure = $this->getPost('sure', 0);
        $starttime = $this->getPost('starttime', -1);
        $endtime = $this->getPost('endtime', -1);
        $starthour = $this->getPost('starthour', -1);
        $endhour = $this->getPost('endhour', -1);
        $startmin = $this->getPost('startmin', -1);
        $endmin = $this->getPost('endmin', -1);
        $flg = $this->getPost('flg', -1);
        $modelid = trim($this->getPost('modelid', ""), "\r\n\t ,");
        $info = $this->getPost('info', -1);
        $notice = $this->getPost('notice', -1);
        $worktype = $this->getPost('worktype', -1);
        $perprice = $this->getPost('perprice', -1);
        $price = $this->getPost('price', -1);
        $pricetime = $this->getPost('pricetime', -1);
        $customername = $this->getPost('customername', -1);
        $customertel = $this->getPost('customertel', -1);
        $customerothertel = $this->getPost('customerothertel', -1);
        $customeraddress = $this->getPost('customeraddress', -1);
        $workaddress = $this->getPost('workaddress', -1);
        $hour = $this->getPost('hour', -1);
        $prepricetime = $this->getPost('prepricetime', -1);
        $companyname = $this->getPost('companyname', -1);
        $cost = $this->getPost('cost', -1);
        $tax = $this->getPost('tax', -1);
        $pay = $this->getPost('pay', -1);
        $ret = $this->getPost('ret', -1);
        $profit = $this->getPost('profit', -1);
        
        /*if($modelid == ""){
            $this->ajaxRet(-1, '模特信息必选');
            return;
        }*/
        
        try {
        	$modelid = trim($modelid, "\r\n, ");
        	if($modelid != ''){
	        	$modelids = explode(",", $modelid);
	            $modelname = Model_Model::getNamesByIDs($modelids);
        	}else{
        		$modelname = '';
        	}
            Model_Work::add($username, $starttime, $endtime, $starthour, $endhour, 
                            $flg, $modelname, $info, $notice, $worktype, $perprice, 
                            $price, $pricetime, $customername, $customertel, $customerothertel, 
                            $customeraddress, $hour,$prepricetime,$companyname, $cost, $tax, $pay, $profit, 
                            $workaddress, $sure, $ret, $startmin, $endmin);
            $this->ajaxRet(0, 'success');
            return;
        }catch (Exception $e){
            $this->ajaxRet($e->getCode(), $e->getMessage());
            return;
        }
    }
    
    public function doModifyAction()
    {
    	$workID = $this->getPost('workid', -1);
    	$workinfo = Model_Work::getByID($workID);
        if(empty($workinfo) || !Model_Work::ifAuthModify($workinfo)){
	        $this->notAuthWeb();
	        return;
	    }
        $sure = $this->getPost('sure', 0);
        $starttime = $this->getPost('starttime', -1);
        $endtime = $this->getPost('endtime', -1);
        $starthour = $this->getPost('starthour', -1);
        $endhour = $this->getPost('endhour', -1);
        $startmin = $this->getPost('startmin', -1);
        $endmin = $this->getPost('endmin', -1);
        $flg = $this->getPost('flg', -1);
        $modelid = trim($this->getPost('modelid', ""), "\r\n\t ,");
        $info = $this->getPost('info', -1);
        $notice = $this->getPost('notice', -1);
        $worktype = $this->getPost('worktype', -1);
        $perprice = $this->getPost('perprice', -1);
        $price = $this->getPost('price', -1);
        $pricetime = $this->getPost('pricetime', -1);
        $customername = $this->getPost('customername', -1);
        $customertel = $this->getPost('customertel', -1);
        $customerothertel = $this->getPost('customerothertel', -1);
        $customeraddress = $this->getPost('customeraddress', -1);
        $workaddress = $this->getPost('workaddress', -1);
		$hour = $this->getPost('hour', -1);
        $prepricetime = $this->getPost('prepricetime', -1);
        $companyname = $this->getPost('companyname', -1);
        $cost = $this->getPost('cost', -1);
        $tax = $this->getPost('tax', -1);
        $pay = $this->getPost('pay', -1);
        $ret = $this->getPost('ret', -1);
        $profit = $this->getPost('profit', -1);
        
        /*if($modelid == ""){
            $this->ajaxRet(-1, '模特信息必选');
            return;
        }*/
        
        try {
        	$modelid = trim($modelid, "\r\n, ");
        	if($modelid != ''){
	        	$modelids = explode(",", $modelid);
	            $modelname = Model_Model::getNamesByIDs($modelids);
        	}else{
        		$modelname = '';
        	}
            Model_Work::update($workID, $starttime, $endtime, $starthour, $endhour, 
                            $flg, $modelname, $info, $notice, $worktype, $perprice, 
                            $price, $pricetime, $customername, $customertel, $customerothertel, 
                            $customeraddress, $hour, $prepricetime, $companyname, $cost, $tax, $pay, 
                            $profit, $workaddress, $sure, $ret, $startmin, $endmin);
            $this->ajaxRet(0, 'success');
            return;
        }catch (Exception $e){
            $this->ajaxRet($e->getCode(), $e->getMessage());
            return;
        }
    }
    
    public function doDelAction()
    {
        $workID = $this->getPost('workid', -1);
    	$workinfo = Model_Work::getByID($workID);
        if(empty($workinfo) || Model_User::getLevelID() != 5){
	        $this->notAuthAjax();
	        return;
	    }

        try {
            Model_Work::delete($workID);
            $this->ajaxRet(0, 'success');
        }catch (Exception $e){
            $this->ajaxRet($e->getCode(), $e->getMessage());
            return;
        }
    }
    
    public function doTimeChangeAction()
    {
        $starttime = $this->getPost('starttime', -1);
        $endtime = $this->getPost('endtime', -1);
        $starthour = $this->getPost('starthour', -1);
        $endhour = $this->getPost('endhour', -1);
        $startmin = $this->getPost('startmin', -1);
        $endmin = $this->getPost('endmin', -1);
        $modelname = $this->getPost('modelids', -1);
        $workid = $this->getPost('workid', -1);
        $flg = $this->getPost('flg', -1);
        
        try {
            $models = Model_Model::getAllModleState($starttime, $endtime, $starthour, $endhour, $startmin, $endmin, $workid);
            $modelHtml = $this->_convertModelsToHtml($models, $modelname, $flg);
            $this->ajaxRet(0, $modelHtml);
        }catch (Exception $e){
            $this->ajaxRet($e->getCode(), $e->getMessage());
            return;
        }
    }
    
    private function _convertModelsToHtml($models, $modelid, $flg)
    {
    	$host = HOST;
    	$modelids = explode(",", $modelid);
    	$header = "";
    	foreach ($models as $model){
 			$header .= "<div style='margin-top:4px;'><div>";
 			if($model['disable'] == true || $model['haswork'] == true || ($flg == 1 && ($model['haswork'] == true || $model['hasface'] == true))){
 				$header .= "<input type='checkbox' name='modelid' disabled value='{$model['modelid']}'>";
 			}else{
				if(in_array($model['modelid'], $modelids, true)){
					$header .= "<input type='checkbox' name='modelid' checked value='{$model['modelid']}'>";
				}else{
					$header .= "<input type='checkbox' name='modelid' value='{$model['modelid']}'>";
				}
 			}
			$header .= "<span style='position:relative;left:1px;top:-3px;'>{$model['modelname']}</span></div>";

			if($model['title'] == true){
				$header .= "<div id='conflict' style='margin-bottom:10px;'><div id='conflictinfo' style='margin-top:3px;padding:3px;border:2px solid #FFFFFF;width:430px;'>";
				if($model['illshow'] != ""){
					$header .= "<div style='margin-top:5px;'><span>{$model['illshow']}</span><span style='float:right;margin-right:15px;'><a target='_blank' href='http://{$host}//model/detail/modelid/{$model['modelid']}' >查看</a></span></div>";
				}
				if($model['beijingshow'] != ""){
					$header .= "<div style='margin-top:5px;'><span>{$model['beijingshow']}</span><span style='float:right;margin-right:15px;'><a target='_blank' href='http://{$host}//model/detail/modelid/{$model['modelid']}' >查看</a></span></div>";
				}
				foreach($model['work'] as $work){
					
				$header .= "<div style='margin-top:8px;'><span>{$work['time']}</span><span style='float:right;margin-right:15px;'><a target='_blank' href='http://{$host}//work/detail/workid/{$work['workid']}' >查看</a></span></div>";
				}
				$header .= "</div></div>";
			}
    		$header .= "</div>";
    	}
    	
    	if($header == ""){
    		$header = "<div style='margin-top:5px;color:#FF0000'>暂无模特信息</div>";
    	}
    	
    	return $header;
    }
}




